/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beanControl;

import Utils.Helper;
import Utils.JDBCHelper;
import Utils.StringUtility;
import beanInfo.interest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;

/**
 *
 * @author lyan
 */
public class interestControl {

    ArrayList<String> listColumn = new ArrayList<String>();
    ArrayList<String> listValue = new ArrayList<String>();
    interest in = new interest();

    public void getlistInterst(JTable table) {

        String sql = "select * from Interest";
        String[] header = {"InterestID", "UpdateDate", "LimitInterset", "UnLimitInterest", "InterestTypeID","Years","PurposesID", "Status"};
        Helper.loadDataTable(sql, table, header);

    }

    public void insertInterst(interest in) {
        Object[] values = {in.getUpdateDate(), in.getLimitInterset(),
            in.getUnLimitInterest(), in.getInterestTypeID(),in.getYears(),in.getPurposesID() ,in.getStatus()};
        JDBCHelper.excuteInsertPlaceHolder("Interest", 7, values);
        Helper.successMessage("Insert  successfull", "wanning");

    }

    public void updateCusroner(interest in) {
        String[] objColumn = {"UpdateDate,LimitInterset,UnLimitInterest,InterestTypeID,PurposeID, Status"};
        String[] objValues = {in.getUpdateDate(), String.valueOf(in.getLimitInterset()), String.valueOf(in.getUnLimitInterest()),
            String.valueOf(in.getInterestTypeID()),String.valueOf(in.getYears()),String.valueOf(in.getPurposesID()), in.getStatus()};


        listColumn = (ArrayList<String>) StringUtility.ArrayToArrayList(objColumn);
        listValue = (ArrayList<String>) StringUtility.ArrayToArrayList(objValues);
        JDBCHelper.executeUpdate("Interest", listColumn, listValue, "InterestID", String.valueOf(in.getInterestID()));
        Helper.successMessage("Update Sucessful", "furniture");
    }

    private int checkDelteInterst(String values) {

        return JDBCHelper.excuteQueryReturnInt("InterestID", "LoanProfile ", values);

    }

    private int checkDeleteInte(String value) {
        return JDBCHelper.excuteQueryReturnInt("InterestID", "Purposes", value);
    }

    public void DeleteCategories(interest in) {
        if (checkDelteInterst(String.valueOf(in.getInterestID())) > 0) {
            Helper.inforMessage("Can not detlete this User", "Wanning");
        } else if (checkDeleteInte(String.valueOf(in.getInterestID())) > 0) {
            Helper.inforMessage("Can not Deletethis user", "wanning");
        } else {
            JDBCHelper.excuteDelete("Interest", "InterestID", String.valueOf(in.getInterestID()));
            Helper.successMessage("Delete successfull", "wanning");
        }

    }
      public int getMaxPurposesID() {
        int id = 0;
        try {
            String sql = "select max (PurposesID) from Purposes";
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            if (rs.next()) {
                id = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(purposesControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }
}
